package com.ichart.doc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * wrap of component
 * create at 2012-4-25 下午11:47:04
 * @author wanghe
 */
public class Component {
	private String name;
	private int type;
	private String className;
	private String extend;
	private String overview;
	List<Item> items = new ArrayList<Item>();
	List<Event> events = new ArrayList<Event>();
	List<Method> methods = new ArrayList<Method>();
	public static Pattern CONFIGRUATION = Pattern.compile("/\\*\\*.*?@cfg.*?\\{(.*?)\\}(.*?)\\*/.*?(\\w*)\\s*:", Pattern.DOTALL);
	public static Pattern ONEROW = Pattern.compile("^\\s*\\*?(.*?)$",Pattern.MULTILINE);
	public static Pattern EVENT = Pattern.compile("/\\*\\*(.*?)'(\\w{5,20})'", Pattern.DOTALL);
	
	
	public void registerMethod(String name,String desc){
		methods.add(new Method(desc.trim(),
				name,
				ComponentUtils.getClassName(this.name),
				this.name));
	}
	
	public void registerEvent(String desc){
		Matcher matcher = EVENT.matcher(desc);
		while(matcher.find()){  
			events.add(new Event(ONEROW.matcher(matcher.group(1).replaceAll("\\*", "").replaceAll("/", "")).replaceAll("$1"),
					matcher.group(2),
					ComponentUtils.getClassName(this.name),
					this.name));
        }
	}
	
	public void config(String desc){
		Matcher matcher_ = CONFIGRUATION.matcher(desc);
		while(matcher_.find()){  
			items.add(new Item(matcher_.group(3).trim(),matcher_.group(1).trim(),ONEROW.matcher(matcher_.group(2).trim()).replaceAll("$1"),ComponentUtils.getClassName(this.name),this.name));
        } 
	}
	
	public Iterator<Item> iterator(){
		return items.iterator();
	}
	
	public List<Item> getItems(){
		return items;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if(name.startsWith("@chart#")){
			name = name.substring(7);
			this.setType(1);
		}
		this.name = name;
		this.setClassName(ComponentUtils.getClassName(this.name));
	}
	public String getExtend() {
		return extend;
	}
	public void setExtend(String extend) {
		this.extend = extend;
	}
	
	public void setItems(List<Item> items) {
		this.items = items;
	}

	public String getOverview() {
		return overview;
	}

	public void setOverview(String overview) {
		overview = overview.substring(0,overview.length()-1);
		this.overview = overview;
	}

	@Override
	public String toString() {
		return "Component [extend=" + extend + ", name=" + name + "]";
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<Event> getEvents() {
		return events;
	}

	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public List<Method> getMethods() {
		return methods;
	}

	public void setMethods(List<Method> methods) {
		this.methods = methods;
	}
	
	
}
